import { defineStore } from "pinia";
import { ref } from "vue";

export const useUserStore = defineStore("user", () => {
  const state = ref({
    name: "未登录",
    role: "guest",
  });
  const getter = {
    getUserName() {
      return state.value.name;
    },
  };
  const actions = {
    async setLoginUser(userInfo) {
      state.value = userInfo;
      sessionStorage.setItem("user", JSON.stringify(userInfo));
    },
    logout() {
      sessionStorage.clear();
      state.value = {
        name: "未登录",
        role: "guest",
      };
    }
  };

  return { state, getter, actions };
});
